'use strict'

class userLogin {

    __mid() {
        return [
            {
                name: 'user-verify',
                path: ['post', 'delete']
            }
        ]
    }

    constructor() {
        this.param = '/'
    }

    async post(c) {
        try {
            var openid = c.box.user.openid;
            var userObj = JSON.parse(c.body.toString('utf-8')).userInfo;
            var sqltext2 = `select * from users where openid = '${openid}'`
            var r2 = await c.service.psql.query(sqltext2);
            // 不是第一次授权
            if (r2.rowCount == 1) {
                return c.send({
                    result: 'seccess',
                    user: r2.rows[0]
                })
            }
            // 第一次授权
            if (r2.rowCount == 0) {
                var sqltext3 = 'select MAX(user_id) from users';
                var r3 = await c.service.psql.query(sqltext3);
                var count = r3.rows[0].max;
                if (count == null) {
                    count = 0;
                }
                count++;
                var user_id = count;

                var sqltext4 = `INSERT INTO users (user_id,openid,user_headshot,`
                    + `user_nickname,user_sex,user_techo,user_matting,user_coin,user_collected_template,`
                    + `user_pay_template,user_pay_sticker) `
                    + `values(${user_id},'${openid}','${userObj.avatarUrl}',`
                    + `'${userObj.nickName}',${userObj.gender},'user_${user_id}_techo',`
                    + `'user_${user_id}_matting',100,'user_${user_id}_coll_tem','user_${user_id}_pay_tem','user_${user_id}_pay_sti')`
                await c.service.psql.query(sqltext4);
                var sqltext5 = `CREATE TABLE user_${user_id}_techo (user_id int,openid varchar(100),techo_id int PRIMARY key);`
                    // + `CREATE TABLE user_${user_id}_task (user_id int,template_id int PRIMARY key);`
                    // + `CREATE TABLE user_${user_id}_coll_tem (user_id int,template_id int PRIMARY key);`
                    + `CREATE TABLE user_${user_id}_pay_sti (user_id int,openid varchar(100), sticker_id int PRIMARY key);`
                    //     + `CREATE TABLE user_${user_id}_matting (user_id int,matting_id int PRIMARY key);`
                    + `CREATE TABLE user_${user_id}_pay_tem (user_id int,openid varchar(100), template_id int PRIMARY key);`
                await c.service.psql.query(sqltext5);
                return c.send({
                    result: 'success①',
                    user: {
                        user_coin: 0,
                        user_collected_template: `user_${user_id}_coll_tem`,
                        user_headshot: userObj.avatarUrl,
                        openid: openid,
                        user_matting: `user_${user_id}_matting`,
                        user_nickname: userObj.nickName,
                        user_pay_sticker: `user_${user_id}_pay_sti`,
                        user_pay_template: `user_${user_id}_pay_tem`,
                        user_sex: userObj.gender,
                        user_sticker_id: 10,
                        user_techo: `user_${user_id}_techo`,
                        user_turncord_day: 0
                    }
                })
            }
            else {
                return c.status(500).send('数据库错误')
            }
        } catch (err) {
            console.log(err);
            return c.status(500).send('other error')
        }
    }
}

module.exports = userLogin